package terminal.gui;

import java.util.UUID;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.wizard.Wizard;

import terminal.communication.CommunicationService;
import terminal.utils.BeanProvider;
import core.exceptions.CommunicationInitiationException;
import core.exceptions.PortInUseException;

public class WzdLocatorUri extends Wizard {

	private WzdPgLocatorUri wzdPgLocatorUri;

	private CommunicationService communicationService = BeanProvider.communicationService();

	public WzdLocatorUri() {
		setWindowTitle("Define Locator URI");
	}

	@Override
	public void addPages() {
		wzdPgLocatorUri = new WzdPgLocatorUri();
		addPage(wzdPgLocatorUri);
	}

	@Override
	public boolean performFinish() {
		wzdPgLocatorUri.update();
		String serverIp = wzdPgLocatorUri.getTerminalIp();
		String serverPort = wzdPgLocatorUri.getTerminalPort();
		String clientIp = wzdPgLocatorUri.getRkwIp();
		String clientPort = wzdPgLocatorUri.getRkwPort();

		UUID communicationToken = wzdPgLocatorUri.getCommunicationToken();

		ConnectionStatusWindow connectionStatusWindow = new ConnectionStatusWindow(getShell());

		try {
			communicationService.setUp(serverIp, serverPort, clientIp, clientPort, communicationToken, connectionStatusWindow);
			connectionStatusWindow.open();
			if (connectionStatusWindow.getReturnCode() == IDialogConstants.OK_ID) {
				return true;
			} else {
				return false;
			}
		} catch (CommunicationInitiationException | PortInUseException e) {
			e.printStackTrace();
			return false;
		}
	}
}
